<?php

namespace app\common\service\user;

use app\admin\model\user\UserCollection;
use app\common\service\BaseService;

class UserCollectionService extends BaseService
{
    /**
     * Author:Zhongyuan Lu
     * 收藏商品
     * @param $uid
     * @param $product_id
     * @return array
     */
    public static function collection($uid,$product_id)
    {
        $data = UserCollection::where('uid',$uid)->where('product_id',$product_id)->find();
        if ($data) {
            $bool = UserCollection::destroy($data->id);
            if($bool) {
                return self::success('取消收藏');
            }
            return self::fail('取消收藏失败');
        }

        $bool = UserCollection::create(['uid'=>$uid,'product_id' => $product_id]);
        if($bool) {
            return self::success('收藏成功');
        }
        return self::fail('收藏失败');
    }

    /**
     * Author:Yingbo Sun
     * 我的收藏
     * @param $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function collectionList($uid)
    {
        if (empty($uid)){
            return self::fail('参数错误');
        }
        $data = UserCollection::where('uid',$uid)->with('goodsCollection')->select();
        $data && $data = $data->toArray();
        return self::success('获取成功',$data);
    }
}